package com.meepo.feed.old.video.shortvideo

import android.content.pm.ActivityInfo
import android.view.View
import android.widget.FrameLayout
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.meepo.basic.schema.feed.Item
import com.meepo.feed.R
import com.meepo.feed.video.fullscreen.Utils
import com.meepo.feed.video.fullscreen.cache.ProxyVideoCacheManager
import com.meepo.framework.design.recycler.ViewHolder
import xyz.doikki.videocontroller.StandardVideoController
import xyz.doikki.videocontroller.component.*
import xyz.doikki.videoplayer.ijk.IjkPlayer
import xyz.doikki.videoplayer.player.VideoView
import xyz.doikki.videoplayer.player.VideoViewManager

class PlayerListManager(val context: FragmentActivity) : LinearLayoutManager(context) {

    private var mVideoView: VideoView<IjkPlayer> = VideoView(context)
    private var mController: StandardVideoController
    private var mTitleView: TitleView = TitleView(context)

    /**
     * 当前播放的位置
     */
    private var mCurPos = -1

    /**
     * 上次播放的位置，用于页面切回来之后恢复播放
     */
    private var mLastPos = mCurPos

    init {
        mVideoView.setOnStateChangeListener(object : VideoView.SimpleOnStateChangeListener() {
            override fun onPlayStateChanged(playState: Int) {
                //监听VideoViewManager释放，重置状态
                if (playState == VideoView.STATE_IDLE) {
                    Utils.removeViewFormParent(mVideoView)
                    mLastPos = mCurPos
                    mCurPos = -1
                }
            }
        })
        mController = StandardVideoController(context)
        mController.addControlComponent(ErrorView(context))
        mController.addControlComponent(CompleteView(context))
//        mController.addControlComponent(mTitleView)

        mController.addControlComponent(VodControlView(context))
        mController.addControlComponent(GestureView(context))
        mController.setEnableOrientation(true)

        mVideoView.setVideoController(mController)
    }


    fun play(position: Int, item: Item, holder: ViewHolder) {
        if (mCurPos == position) return
        if (mCurPos != -1) {
            release()
        }

        val playUrl = item.content?.content
        //边播边存
        val proxyUrl = ProxyVideoCacheManager.getProxy(context).getProxyUrl(playUrl)
        mVideoView.setUrl(proxyUrl)
        mTitleView.setTitle(item.title)

        //把列表中预置的PrepareView添加到控制器中，注意isPrivate此处只能为true。
        mController.addControlComponent(holder.getView<PrepareView>(R.id.prepare_view), true)

        Utils.removeViewFormParent(mVideoView)
        holder.getView<FrameLayout>(R.id.player_container)?.addView(mVideoView, 0)
        //播放之前将VideoView添加到VideoViewManager以便在别的页面也能操作它

        VideoViewManager.instance().add(mVideoView, "list")
        mVideoView.start()

        mCurPos = position
    }

    fun pause() {
        mVideoView.pause()
    }

    fun resume() {
        mVideoView.resume()
    }

    fun release() {
        mVideoView.release()
        if (mVideoView.isFullScreen) {
            mVideoView.stopFullScreen()
        }
        context.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

        mCurPos = -1
    }


    fun releaseIfDetachedFromWindow(v: View?) {

        if (v != null && v === mVideoView && !mVideoView.isFullScreen) {
            release()
        }
    }

}